package jdbc;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author acc8226
 */
public class JDBCTutorialUtilities {
    public String dbms;
    public String dbName;
    public String userName;
    public String password;
    public String urlString;

    private String serverName;
    private int portNumber;

    public JDBCTutorialUtilities(String propertiesFileName) throws
            IOException {
        super();
        this.setProperties(propertiesFileName);
    }

    public static void printSQLException(SQLException ex) {
        for (Throwable e : ex) {
            if (e instanceof SQLException) {
                e.printStackTrace(System.err);
                System.err.println("SQLState: " + ((SQLException) e).getSQLState());
                System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
                System.err.println("Message: " + e.getMessage());
                Throwable t = ex.getCause();
                while (t != null) {
                    System.out.println("Cause: " + t);
                    t = t.getCause();
                }

            }
        }
    }

    private void setProperties(String fileName) throws
            IOException {
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream(fileName);
        prop.loadFromXML(fis);

        this.dbms = prop.getProperty("dbms");
        this.dbName = prop.getProperty("database_name");
        this.userName = prop.getProperty("user_name");
        this.password = prop.getProperty("password");
        this.serverName = prop.getProperty("server_name");
        this.portNumber = Integer.parseInt(prop.getProperty("port_number"));

        System.out.println("Set the following properties:");
        System.out.println("dbms: " + dbms);
        System.out.println("dbName: " + dbName);
        System.out.println("userName: " + userName);
        System.out.println("serverName: " + serverName);
        System.out.println("portNumber: " + portNumber);
    }

    public Connection getConnection() throws SQLException {
        Connection conn = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user", this.userName);
        connectionProps.put("password", this.password);

        String currentUrlString;
        if (this.dbms.equals("mysql")) {
            currentUrlString = "jdbc:" + this.dbms + "://" + this.serverName +
                    ":" + this.portNumber + "/";
            conn =
                    DriverManager.getConnection(currentUrlString, connectionProps);

            this.urlString = currentUrlString + this.dbName;
            conn.setCatalog(this.dbName);
        }
        System.out.println("Connected to database");
        return conn;
    }

    public static void closeConnection(Connection connArg) {
        System.out.println("Releasing all open resources ...");
        try {
            if (connArg != null) {
                connArg.close();
            }
        } catch (SQLException sqle) {
            printSQLException(sqle);
        }
    }
}
